package org.steven.crm.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author wuzw
 * @Date 2023/5/25 9:55
 * @Version 1.0
 */
public class JSONUtil {
    public static Map<String, String> toMap(Object obj) {
        if (obj == null) {
            return null;
        }
        String jsonStr = JSON.toJSONString(obj);
        return toMap(jsonStr);
    }

    public static Map<String, String> toMap(String jsonStr) {
        if (StringUtils.isEmpty(jsonStr)) {
            return null;
        }
        Map<String, String> map = JSON.parseObject(jsonStr, new TypeReference<Map>(){});

        return map;
    }

    public static <T> T toBean(Map map, Class<T> c) {
        String jsonStr = JSON.toJSONString(map);
        return toBean(jsonStr, c);
    }

    public static <T> T toBean(String jsonStr, Class<T> c) {
        T data = JSON.parseObject(jsonStr, c);

        return data;
    }
}
